
package com.gemom.gui;

import com.gemom.util.*;

import java.awt.*;
import javax.swing.*;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.axis.ValueAxis;

/**
 *
 * @author Yair Diaz
 */
public class ChartActualDataGUI extends JPanel{

     final JFreeChart chart;
     TimeSeries []series;
     String []nameSeries;
     ActualDataSeries []actuaSeries;
     XYPlot plot;
    
    public ChartActualDataGUI( String []nameSeries)
    {
        this.nameSeries=nameSeries;
        
        series= new TimeSeries[nameSeries.length];
        for(int i=0;i<nameSeries.length;i++)
            {series[i] = new TimeSeries(nameSeries[i], Minute.class);}
    
         actuaSeries= new ActualDataSeries[nameSeries.length];
         for(int i=0;i<nameSeries.length;i++)
            {actuaSeries[i]=new ActualDataSeries(nameSeries[i]);
                 for(int j=0;j<actuaSeries[i].getDates().length;j++)//actuaSeries[i].getDates().length
                 {series[i].add(new Minute(actuaSeries[i].getDates()[j]),
                          actuaSeries[i].getMsgPerMin()[j]);}
            }
        
        
        
        TimeSeriesCollection dataset = new TimeSeriesCollection();
        for(int i=0;i<nameSeries.length;i++)dataset.addSeries(series[i]);
        
        chart = createChart(dataset);
        final ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(1200, 280));
        
        this.setLayout(new BorderLayout());
        this.add(chartPanel);
    }
    
    private JFreeChart createChart(final TimeSeriesCollection dataset) {
    
        JFreeChart chart = ChartFactory.createTimeSeriesChart(
            "Share Market Data",  "Time",  "Messages per minute", 
            dataset, true, true,  false );
        
        plot = chart.getXYPlot();
        ValueAxis axis = plot.getDomainAxis();
        axis.setAutoRange(true);
        axis = plot.getRangeAxis();
        axis.setAutoRange(true);
        
        this.hidePlot();
        
        return chart;
    }
    
    public double[][] getSeriesData(){
        //rows:samples  columns:series
        double[][] seriesData=new double[series[0].getItemCount()][series.length];
       
        for(int i=0;i<series.length;i++)
        {for(int j=0;j<series[0].getItemCount();j++)
            {seriesData[j][i]=series[i].getValue(j).doubleValue();}}
        return seriesData;
    }
    
    public void hidePlot()
        { for(int i=0;i<nameSeries.length;i++)
            plot.getRenderer(0).setSeriesVisible(i, false); }
    
    public void showPlot()
        { for(int i=0;i<nameSeries.length;i++)
            plot.getRenderer(0).setSeriesVisible(i, true); }
    
    
}


